package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;
import dto.BookDTO;
import dto.LogonDTO;
import dto.SearchBookDTO;

public class SearchBookPage extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel panel;
	private JPanel panelResults;

	private JCheckBox chckbxTitle;
	private JCheckBox chckbxAuthor;
	private JCheckBox chckbxCatalogNumber;
	private JCheckBox chckbxISBN;

	private JLabel labelMessage;
	private JTextField textFieldTitle;
	private JTextField textFieldAuthor;
	private JTextField textFieldCatalog;
	private JTextField textFieldISBN;

	private JTextField textFieldEditorUsername;
	private JTextField textFieldEditorLevel;

	private JButton btnSearchBook = new JButton("Search Book");

	private LogonDTO logonDTO;
	private SearchBookDTO searchBookDTO;
	private List<BookDTO> searchedBooks;
	private String mode;

	public LogonDTO getLogonDTO() {
		return logonDTO;
	}

	public SearchBookDTO getSearchBookDTO() {
		searchBookDTO.setISBN(textFieldISBN.getText().trim());
		searchBookDTO.setTitle(textFieldTitle.getText().trim());
		searchBookDTO.setAuthor(textFieldAuthor.getText().trim());
		searchBookDTO.setDeweyDecimalNo(textFieldCatalog.getText().trim());
		searchBookDTO.setCheckedTitle(chckbxTitle.isSelected());
		searchBookDTO.setCheckedAuthor(chckbxAuthor.isSelected());
		searchBookDTO.setCheckedISBN(chckbxISBN.isSelected());
		searchBookDTO.setCheckedCatalogNo(chckbxCatalogNumber.isSelected());

		return searchBookDTO;
	}

	public List<BookDTO> getSearchedBooks() {
		return searchedBooks;
	}

	public void setSearchedBooks(List<BookDTO> searchedBooks) {
		this.searchedBooks = searchedBooks;
	}

	/**
	 * Create the dialog.
	 */
	public SearchBookPage(LogonDTO logonDTO, String mode) {

		this.logonDTO = logonDTO;

		this.mode = mode;
		searchBookDTO = new SearchBookDTO();

		setBounds(100, 100, 509, 576);
		getContentPane().setLayout(new BorderLayout());
		{
			panel = new JPanel();
			getContentPane().add(panel, BorderLayout.NORTH);
			panel.setLayout(new MigLayout("", "[grow][grow][][grow][]",
					"[][][][][][][][][][]"));
			{
				JLabel lblJaLibrary = new JLabel("J&A Library");
				lblJaLibrary.setForeground(Color.BLUE);
				lblJaLibrary.setFont(new Font("Arial", Font.BOLD, 16));
				panel.add(lblJaLibrary, "cell 0 0 5 1,alignx center");
			}
			{
				JLabel lblEditdeleteBook = new JLabel("Edit/Delete Book");
				panel.add(lblEditdeleteBook, "cell 0 1 5 1,alignx center");
			}
			{
				JLabel label = new JLabel("     ");
				panel.add(label, "cell 0 2");
			}
			{
				chckbxTitle = new JCheckBox("Title:");
				chckbxTitle.setSelected(true);
				panel.add(chckbxTitle, "cell 0 3");
			}
			{
				textFieldTitle = new JTextField();
				textFieldTitle.setDocument(new JTextFieldLimit(100));
				panel.add(textFieldTitle, "cell 1 3 2 1,growx");
				textFieldTitle.setColumns(10);
			}

			// SEARCH BOOK
			panel.add(btnSearchBook, "cell 4 3,growx");

			{
				chckbxAuthor = new JCheckBox("Author:");
				chckbxAuthor
						.addPropertyChangeListener(new PropertyChangeListener() {
							public void propertyChange(PropertyChangeEvent arg0) {
							}
						});
				panel.add(chckbxAuthor, "cell 0 4");
			}
			{
				textFieldAuthor = new JTextField();
				textFieldAuthor.setDocument(new JTextFieldLimit(100));
				panel.add(textFieldAuthor, "cell 1 4 2 1,growx");
				textFieldAuthor.setColumns(10);
			}
			{
				chckbxCatalogNumber = new JCheckBox("Catalog Number:");
				chckbxCatalogNumber.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent arg0) {
						if (chckbxCatalogNumber.isSelected()) {
							chckbxTitle.setSelected(false);
							chckbxAuthor.setSelected(false);
							chckbxISBN.setSelected(false);
							chckbxTitle.setEnabled(false);
							chckbxAuthor.setEnabled(false);
							chckbxISBN.setEnabled(false);
						} else {
							chckbxTitle.setEnabled(true);
							chckbxAuthor.setEnabled(true);
							chckbxISBN.setEnabled(true);
						}
					}
				});
				chckbxCatalogNumber
						.addPropertyChangeListener(new PropertyChangeListener() {
							public void propertyChange(PropertyChangeEvent arg0) {

							}
						});
				chckbxCatalogNumber.addChangeListener(new ChangeListener() {
					public void stateChanged(ChangeEvent arg0) {

					}
				});
				panel.add(chckbxCatalogNumber, "cell 0 5");
			}
			{
				textFieldCatalog = new JTextField();
				textFieldCatalog.setDocument(new JTextFieldLimit(20));
				panel.add(textFieldCatalog, "cell 1 5,growx");
				textFieldCatalog.setColumns(10);
			}
			{
				chckbxISBN = new JCheckBox("ISBN:");
				chckbxISBN.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent arg0) {
						if (chckbxISBN.isSelected()) {
							chckbxTitle.setSelected(false);
							chckbxAuthor.setSelected(false);
							chckbxTitle.setEnabled(false);
							chckbxAuthor.setEnabled(false);
						} else {
							chckbxTitle.setEnabled(true);
							chckbxAuthor.setEnabled(true);
						}
					}
				});
				panel.add(chckbxISBN, "cell 0 6");
			}
			{
				textFieldISBN = new JTextField();
				textFieldISBN.setDocument(new JTextFieldLimit(30));
				panel.add(textFieldISBN, "cell 1 6,growx");
				textFieldISBN.setColumns(10);
			}
			{
				JButton btnNewButton_1 = new JButton("Return to Main Menu");
				btnNewButton_1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				panel.add(btnNewButton_1, "cell 3 7 2 1,growx");
			}
			{
				textFieldEditorUsername = new JTextField();
				textFieldEditorUsername.setEditable(false);
				textFieldEditorUsername.setText(logonDTO.getUserName());
				panel.add(textFieldEditorUsername, "cell 0 8 3 1,growx");
				textFieldEditorUsername.setColumns(10);
			}
			{
				textFieldEditorLevel = new JTextField();
				textFieldEditorLevel.setEditable(false);
				textFieldEditorLevel.setText(logonDTO.getUserTitle());
				panel.add(textFieldEditorLevel, "cell 3 8 2 1,growx");
				textFieldEditorLevel.setColumns(10);
			}
			{
				labelMessage = new JLabel("     ");
				labelMessage.setForeground(Color.BLUE);
				panel.add(labelMessage, "cell 0 9");
			}
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			getContentPane().add(scrollPane, BorderLayout.CENTER);
			{
				panelResults = new JPanel();
				scrollPane.setViewportView(panelResults);
				drawBottomPanelHeader(panelResults);
			}
		}
	}

	private void drawBottomPanelHeader(JPanel panel) {
		if (panel.getComponentCount() > 0) {
			panel.removeAll();
			System.out.println("Removed all");
		}
		panel.setLayout(new MigLayout("", "[][grow][][][][]", "[][]"));

	}

	public void reDrawThis(ActionListener editBookListener,
			ActionListener deleteBookListener) {

		panelResults.removeAll();
		int selectedBookIndex = 1;

		for (BookDTO searchedBook : searchedBooks) {
			String ddc = searchedBook.getDeweyDecimalNo();
			String title = searchedBook.getTitle();

			JLabel label = new JLabel(selectedBookIndex + ".");
			panelResults.add(label, "flowx,cell 0 " + selectedBookIndex);

			JButton btnEditBook = new JButton("[" + ddc + "] " + title);
			btnEditBook.setActionCommand(String.valueOf(selectedBookIndex - 1));
			btnEditBook.addActionListener(editBookListener);
			btnEditBook.setFont(new Font("Arial", Font.PLAIN, 11));
			panelResults.add(btnEditBook, "cell 1 " + selectedBookIndex
					+ " 4 1,growx");

			if (mode == "EDIT") {
				JButton btnDelete = new JButton("Delete");
				btnDelete.setActionCommand(String
						.valueOf(selectedBookIndex - 1));
				btnDelete.addActionListener(deleteBookListener);
				panelResults.add(btnDelete, "cell 5 " + selectedBookIndex
						+ ",alignx right");
			}

			panelResults.revalidate();
			panelResults.repaint();

			selectedBookIndex++;
		}
	}

	public void addSearchBookButtonListener(ActionListener al) {
		drawBottomPanelHeader(panelResults);
		btnSearchBook.addActionListener(al);
	}

	public int showConfirmDialog(String bookCatalog) {

		return JOptionPane.showConfirmDialog(panel,
				"Are you sure you want to delete this book?\n" + bookCatalog,
				"Confirm Delete", JOptionPane.YES_NO_CANCEL_OPTION);
	}

	public void displayMessage(String message) {
		labelMessage.setText(message);
	}
}
